const BehaviorAll = require("../../../behaviors/behavior_all.js");
const ServiceOrder = require("../../../service/service_order.js");
const BehaviorAddress = require("../../../template/address/behavior.js");

module.exports = {
  /**
   * behaviors
   */
  behaviors: [BehaviorAll, BehaviorAddress],
  /**
   * 页面的初始数据
   */
  data: {
    triggerRetrieveData: false,
    triggerSubmitFlag: false,
    car_amount: 0,
    car_allPrice: 0
  },
  /**
   * 方法
   */
  methods: {
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      const _this = this;
    },
    /**
     * onShow
     */
    onShow: function (e) {
      const _this = this;
      _this.setData({ triggerRetrieveData: true });
    },
    /**
     * 获取购物车数据
     */
    car_onRetrieve: function (e) {
      const _this = this;
      _this.setData({
        car_amount: e.detail.amount,
        car_allPrice: e.detail.total
      })
    },
    /**
     * 数据改变
     */
    car_onChange: function (e) {
      const _this = this;
      _this.setData({
        car_amount: e.detail.amount,
        car_allPrice: e.detail.total
      })
    },
    /**
     * 激活提交
     */
    onTriggerSubmit: function (e) {
      this.setData({ triggerSubmitFlag: true })
    },
    /**
     * 提交
     */
    car_onSubmit: function(e){
      const _this = this;
      const products = e.detail.selectedData;
      const sendData = [];
      for (let product of products){
        console.log(product)
        sendData.push({ 'id': product.id, 'amount': product.amount})
      }
      //
      ServiceOrder.create(sendData, function(data){
        e.detail.callback();
        wx.navigateTo({
          url: '../orderPay/orderPay?id=' + data.id,
        })
      });
    }

  }

};